package cn.touna.jss.modules.third.xhy;

import cn.touna.risk.api.contraints.facet.First;
import cn.touna.risk.api.enums.SystemErrorCodeEnum;
import cn.touna.jss.common.properties.disconf.XhyProperties;
import cn.touna.jss.common.util.DateUtil;
import cn.touna.jss.common.util.ValidUtil;
import cn.touna.jss.modules.adapter.ApiMapping;
import cn.touna.jss.modules.third.xhy.entity.InternetlevelRequest;
import cn.touna.risk.api.domain.Response;
import cn.touna.risk.api.utils.ServiceContext;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;

/**
 ********************************************************
 * 小黑鱼接口
 * @author zhangjialian
 * @Date 2018-12-13 14:19
 * @Version V1.0
 ********************************************************/
@Service
public class XhyService {
    private static final Logger logger = LogManager.getLogger(XhyService.class);
    @Resource(name = "httpsRestTemplate")
    private RestTemplate restTemplate;
    @Autowired
    XhyProperties xhyProperties;
    /**
     ********************************************************************
     * 互联网等级接口
     * @param request Ocr车辆识别请求对象
     * @Author  zhangjialian
     * @return  cn.touna.risk.api.domain.Response
     * @date    2018/9/12 14:00
     ********************************************************************/
    @ApiMapping(value = "xhy.internetlevel",desc = "小黑鱼.互联网等级接口")
    public Response ocrRecognize(InternetlevelRequest request) throws Exception {
        long currentMills=System.currentTimeMillis();
        logger.info("【互联网等级接口】internetlevel begin");
        //校验
        Response response = ValidUtil.valid(request, First.class);
        if (response != null) {return response;}
        JSONObject queryObj = new JSONObject();
        queryObj.put("partnerCode", xhyProperties.getPartnerCode());//客户编码
        queryObj.put("partnerKey", xhyProperties.getPartnerKey());//客户密钥
        queryObj.put("outTradeNo", ServiceContext.genUniqueId());//ID,最长不超过64位
        queryObj.put("requestTime", DateUtil.dateToStr(new Date(),"yyyyMMddHHmmssSSS"));//yyyyMMddHHmmssSSS
        JSONObject body = new JSONObject();
        body.put("name", request.getName());
        body.put("identity", request.getIdCard());
        queryObj.put("body", body);
        String resultStr =null;
        try {
            resultStr = restTemplate.postForObject(xhyProperties.getInternetlevelUrl(), queryObj,String.class);
            logger.info("【互联网等级接口】end：result={},costTime={}ms", resultStr,(System.currentTimeMillis()-currentMills));
            if (StringUtils.isNotEmpty(resultStr)) {
                JSONObject resultObj = JSONObject.parseObject(resultStr);
                boolean success = resultObj.getBooleanValue("success");
                if (success) {
                    return new Response().success(resultObj);
                } else {
                    return new Response().fail(resultObj.getString("errorCode"), resultObj.getString("msg"));
                }
            }
        }catch (Exception e) {
            logger.error("【互联网等级接口】end,error:", resultStr,e);
        }
        return new Response().fail(SystemErrorCodeEnum.JSS_EXTSYSTEM_RESP_FAIL);
    }

}
